package com.hzcx.app.simplechat.ui.login;

import android.os.Bundle;
import android.text.Editable;
import android.text.TextWatcher;
import android.view.View;
import android.widget.EditText;
import android.widget.ImageView;
import android.widget.TextView;

import com.hzcx.app.simplechat.R;
import com.hzcx.app.simplechat.base.BaseActivity;
import com.hzcx.app.simplechat.bean.UpLoadImgBean;
import com.hzcx.app.simplechat.ui.login.bean.LoginInfoBean;
import com.hzcx.app.simplechat.ui.login.contract.AddUserInfoContract;
import com.hzcx.app.simplechat.ui.login.presenter.AddUserInfoPresenter;
import com.hzcx.app.simplechat.ui.login.util.LoginUtil;
import com.hzcx.app.simplechat.ui.mine.EditUserInfoActivity;
import com.hzcx.app.simplechat.ui.publicui.PublicRoundDialog;
import com.hzcx.app.simplechat.util.empty.EmptyUtils;
import com.hzcx.app.simplechat.util.glide.GlideUtils;
import com.hzcx.app.simplechat.util.photo.PhotoUtil;
import com.hzcx.app.simplechat.view.MainBlueConfirmButton;
import com.luck.picture.lib.entity.LocalMedia;
import com.luck.picture.lib.listener.OnResultCallbackListener;

import java.util.ArrayList;
import java.util.List;

import butterknife.BindView;
import butterknife.ButterKnife;
import butterknife.OnClick;

/**
 * 完善用户资料
 */
public class AddUserInfoActivity extends BaseActivity<AddUserInfoPresenter> implements OnResultCallbackListener<LocalMedia>, AddUserInfoContract.View {

    public static final String INTENT_LOGIN_INFO = "INTENT_LOGIN_INFO";

    @BindView(R.id.tv_title)
    TextView tvTitle;
    @BindView(R.id.iv_back)
    ImageView ivBack;
    @BindView(R.id.iv_title_right)
    ImageView ivTitleRight;
    @BindView(R.id.tv_title_right)
    TextView tvTitleRight;
    @BindView(R.id.iv_head)
    ImageView ivHead;
    @BindView(R.id.tv_tips)
    TextView tvTips;
    @BindView(R.id.et_nick_name)
    EditText etNickName;
    @BindView(R.id.btn_commit)
    MainBlueConfirmButton btnCommit;

    private String head;
    private LoginInfoBean loginInfoBean;

    @Override
    public int getLayout() {
        return R.layout.activity_add_userinfo;
    }

    @Override
    public void initPresenter() {
        mPresenter = new AddUserInfoPresenter();
    }

    @Override
    public void initView() {
        tvTitle.setText("完善资料");
        btnCommit.setText("开启" + getResources().getString(R.string.app_name));

        etNickName.addTextChangedListener(new TextWatcher() {
            @Override
            public void beforeTextChanged(CharSequence charSequence, int i, int i1, int i2) {

            }

            @Override
            public void onTextChanged(CharSequence charSequence, int i, int i1, int i2) {
                checkCommit();
            }

            @Override
            public void afterTextChanged(Editable editable) {

            }
        });
    }

    @Override
    public void initData() {
        loginInfoBean = (LoginInfoBean) getIntent().getSerializableExtra(INTENT_LOGIN_INFO);
    }

    @OnClick({R.id.iv_head, R.id.btn_commit})
    public void viewOnClick(View view) {
        switch (view.getId()) {
            case R.id.iv_head:
                showHead();
                break;

            case R.id.btn_commit: // 提交
                mPresenter.uploadImg(this, head, loginInfoBean.getToken());
                break;
        }
    }

    /**
     * 展示头像弹窗
     */
    private void showHead() {
        List<String> list = new ArrayList<>();
        list.add("拍照");
        list.add("相册");
        PublicRoundDialog roundDialog = new PublicRoundDialog(this, "请选择", list, (pos, menu) -> {
            switch (menu) {
                case "拍照":
                    PhotoUtil.showCameraByHead(AddUserInfoActivity.this, this);
                    break;

                case "相册":
                    PhotoUtil.showPhotoByHead(AddUserInfoActivity.this, this);
                    break;
            }
        });
        roundDialog.show();
    }

    /**
     * 头像选择返回
     *
     * @param result
     */
    @Override
    public void onResult(List<LocalMedia> result) {
        try {
            head = PhotoUtil.getPath(result.get(0));
            GlideUtils.loadImgAsCircle(this, head, ivHead);
            checkCommit();
        } catch (Exception e) {
            e.printStackTrace();
            showError("头像选择错误：" + e.getMessage());
        }
    }

    @Override
    public void onCancel() {

    }

    /**
     * 判断资料是否填写完整
     */
    private void checkCommit() {
        if (EmptyUtils.isNotEmpty(head) && EmptyUtils.isNotEmpty(etNickName.getText().toString())) {
            btnCommit.setBtnEnabled(true);
        } else {
            btnCommit.setBtnEnabled(false);
        }
    }

    /**
     * 头像上传成功
     *
     * @param data
     */
    @Override
    public void uploadResult(UpLoadImgBean data) {
        loginInfoBean.setAvatar(data.getUrl());
        mPresenter.commitUserInfo(this, loginInfoBean.getToken(), data.getUrl(), etNickName.getText().toString());
    }

    /**
     * 资料提交成功
     */
    @Override
    public void commitSuccess() {
        loginInfoBean.setNickname(etNickName.getText().toString());
        loginInfoBean.setIs_perfect(1);
        LoginUtil.loginSuccess(this, loginInfoBean);
    }
}
